﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using QuestionsAnswers.UI.Models;

namespace QuestionsAnswers.UI.Controllers
{
    public class InvitationController : Controller
    {
        private readonly QuestionsAnswersContext _db = new QuestionsAnswersContext();

 
        [Authorize]
        public PartialViewResult InvitedTo()
        {
            String email = Membership.GetUser().Email;
            List<Invitation> invites = _db.Invitations.Where(x => x.InvitedEmail == email).ToList();
            return PartialView("InvitedToPartial", invites);
        }


        public ActionResult Create()
        {
            return View();
        }


        [HttpPost]
        public ActionResult Create(Invitation invitation)
        {
            if (ModelState.IsValid)
            {
                _db.Invitations.Add(invitation);
                _db.SaveChanges();
                return RedirectToAction("Index", "Course");
            }

            return View(invitation);
        }


        public ActionResult Edit(int id)
        {
            Invitation invitation = _db.Invitations.Find(id);
            return View(invitation);
        }

        
        [HttpPost]
        public ActionResult Edit(Invitation invitation)
        {
            if (ModelState.IsValid)
            {
                _db.Entry(invitation).State = EntityState.Modified;
                _db.SaveChanges();
                return RedirectToAction("Index", "Course");
            }
            return View(invitation);
        }


        public ActionResult Delete(int id)
        {
            Invitation invitation = _db.Invitations.Find(id);
            return View(invitation);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Invitation invitation = _db.Invitations.Find(id);
            _db.Invitations.Remove(invitation);
            _db.SaveChanges();
            return RedirectToAction("Index", "Course");
        }

        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }

        public ActionResult AcceptInvite(int id)
        {
            Invitation invitation = _db.Invitations.First(x => x.InvitationId == id);
            return View(invitation);
        }

        [Authorize]
        [HttpPost, ActionName("AcceptInvite")]
        public ActionResult AcceptConfirmed(int id)
        {
            Invitation invitation = _db.Invitations.First(x => x.InvitationId == id);
            string userName = Membership.GetUserNameByEmail(invitation.InvitedEmail);
            if (userName == User.Identity.Name)
            {
                User user = _db.Users.First(x => x.UserName == userName);
                _db.Invitations.Remove(invitation);
                Course course = _db.Courses.First(x => x.CourseId == invitation.CourseId);
                if (invitation.MemberType == "Participant" || invitation.MemberType == "Participants")
                    course.Participants.Add(user);
                else
                    course.Owners.Add(user);

                _db.SaveChanges();
                _db.Entry(course).State = EntityState.Modified;
                _db.SaveChanges();
            }
            return RedirectToAction("Index", "Course");
        }
        public ActionResult InviteMember(CourseMemberIds member)
        {
            return Request.IsAjaxRequest()?(ActionResult) PartialView(member):View(member);
        }

        [Authorize]
        [HttpPost]
        public ActionResult InviteMember(int courseId, string memberType, string email)
        {
            Course course = _db.Courses.Include("Owners").Include("Participants").First(x => x.CourseId == courseId);

            if (course.CanModify(User.Identity.Name))
            {
                course.GetInvitations(email, memberType, _db.Users.First(x => x.UserName == User.Identity.Name)).ForEach(inv => _db.Invitations.Add(inv));
                _db.SaveChanges();
            }
            return RedirectToAction("Index", "Course");
        }
        [Authorize]
        public ActionResult DeclineInvite(int id)
        {
            Invitation invitation = _db.Invitations.First(x => x.InvitationId == id);
            return View(invitation);
        }
        [Authorize]
        [HttpPost, ActionName("DeclineInvite")]
        public ActionResult DeclineConfirmed(int id)
        {
            Invitation invitation = _db.Invitations.First(x => x.InvitationId == id);
            string userName = Membership.GetUserNameByEmail(invitation.InvitedEmail);
            if (userName == User.Identity.Name)
            {
                _db.Invitations.Remove(invitation);
                _db.SaveChanges();
            }
            return RedirectToAction("Index", "Course");
        }
    }
}